package com.readbook.action;


import com.readbook.fileimage.ImgEditor;
import com.readbook.service.LoginService;
import com.readbook.service.WechartService;
import com.readbook.util.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.*;

@Controller
@RequestMapping("/")
public class login {
    @Resource
    private LoginService loginService;
    @Resource
    private WechartService wechartService;

    @RequestMapping("onlogin.html")
    @ResponseBody
    public Map<String, Object> onlogin(HttpServletRequest request, HttpServletResponse response) {
        String uname = request.getParameter("uname").trim();
        String upwd = request.getParameter("upwd").trim();
        Map<String, Object> dlmap = new HashMap<>();
        dlmap.put("uname", uname);
        dlmap.put("upwd", upwd);
        //查询账号权限
        String permission = loginService.getUserPermission(dlmap);
        //根据用户名和密码查询用户是否存在
        List<Map<String, Object>> list1 = loginService.getUserInfoByZhMm(dlmap);
        if (list1.size() > 0) {
            Map<String, Object> rmap = new HashMap<>();
            Map<String, Object> gmap = list1.get(0);
            HttpSession session = ((HttpServletRequest) request).getSession();
            if (gmap.get("USID") != null) {
                session.setAttribute("usid", gmap.get("USID").toString());
            }
            if (gmap.get("NAME") != null) {
                session.setAttribute("name", gmap.get("NAME").toString());
            }
            if (gmap.get("SEX") != null) {
                session.setAttribute("sex", gmap.get("SEX").toString());
            }
            if (gmap.get("ZW") != null) {
                session.setAttribute("identity", gmap.get("ZW").toString());
            }
            if (gmap.get("WSTATE") != null) {//1学生 2教师
                if (gmap.get("WSTATE").toString().equals("1")) {
                    rmap.put("msg", "账号权限不足!");
                    rmap.put("result", false);
                    return rmap;
                }
                session.setAttribute("user_type", gmap.get("WSTATE").toString());
            }
            if (gmap.get("NJ") != null) {
                session.setAttribute("nj", gmap.get("NJ").toString());
            }
            if (gmap.get("BJ") != null) {
                session.setAttribute("bj", gmap.get("BJ").toString());
            }
            rmap.put("usid", gmap.get("USID"));
            rmap.put("name", gmap.get("NAME"));
            rmap.put("sex", gmap.get("SEX"));
            rmap.put("identity", gmap.get("ZW"));
            rmap.put("user_type", gmap.get("WSTATE"));
            rmap.put("nj", gmap.get("NJ"));
            rmap.put("bj", gmap.get("BJ"));
            rmap.put("jsessionid", session.getId());
            rmap.put("uname", uname);
            session.setAttribute("uname", uname);
            rmap.put("result", true);
            if (permission != null && !permission.equals("undefined") && !permission.equals("")) {
                rmap.put("permission", permission);
            } else {
                rmap.put("permission", "null");
            }
            return rmap;
        } else {
            Map<String, Object> rmap = getthirdInfo.getUserInfoThird(uname, upwd);
            if (rmap.get("result").toString().equals("true")) {
                HttpSession session = ((HttpServletRequest) request).getSession();
                if (rmap.get("name") != null) {
                    session.setAttribute("name", rmap.get("name").toString());
                }
                if (rmap.get("sex") != null) {
                    session.setAttribute("sex", rmap.get("sex").toString());
                }
                if (rmap.get("identity") != null) {
                    session.setAttribute("identity", rmap.get("identity").toString());
                }
                if (rmap.get("user_type") != null) {//1学生 2教师
                    session.setAttribute("user_type", rmap.get("user_type").toString());
                }

                if (rmap.get("user_type") != null) {//1学生 2教师
                    if (rmap.get("user_type").toString().equals("1")) {
                        rmap.put("msg", "账号权限不足!");
                        rmap.put("result", false);
                        return rmap;
                    }
                    session.setAttribute("user_type", "2");
                } else {
                    rmap.put("msg", "账号权限不足!");
                    rmap.put("result", false);
                    return rmap;
                }

                if (rmap.get("nj") != null) {
                    session.setAttribute("nj", rmap.get("nj").toString());
                }
                if (rmap.get("bj") != null) {
                    session.setAttribute("bj", rmap.get("bj").toString());
                }
                rmap.put("jsessionid", session.getId());
                rmap.put("uname", uname);
                session.setAttribute("uname", uname);

                Map<String, Object> semap = new HashMap<String, Object>();
                //根据登录账号查询用户是否已经生成了usid
                //根据登录账号查询是否已经有生成的usid
                Map<String, Object> zhmap = new HashMap<String, Object>();
                zhmap.put("dlzh", uname);
                List<Map<String, Object>> listzh = new ArrayList<>();
                listzh = wechartService.getUsidBydlzh(zhmap);
                String usid = "";
                if (listzh.size() > 0) {
                    usid = listzh.get(0).get("USID").toString();
                } else {
                    usid = UUID.randomUUID().toString().replaceAll("-", "");
                }
                session.setAttribute("usid", usid);

                rmap.put("usid", usid);
                semap.put("usid", usid);
                semap.put("name", rmap.get("name"));
                semap.put("nj", rmap.get("nj"));
                semap.put("bj", rmap.get("bj"));
                semap.put("zw", rmap.get("identity"));
                semap.put("wstate", rmap.get("user_type"));//1学生 2教师
                semap.put("dlzh", uname);
                semap.put("dlma", upwd);
                semap.put("phone", rmap.get("phone"));
                semap.put("sex", rmap.get("sex"));
                semap.put("wxid", "0");
                wechartService.insertwxbangding(semap);
            }
            if (permission != null && !permission.equals("undefined") && !permission.equals("")) {
                rmap.put("permission", permission);
            } else {
                rmap.put("permission", "null");
            }
            return rmap;
        }


    }


        @RequestMapping("userInfo.html")
        @ResponseBody
        public Map<String, Object> userInfo(HttpServletRequest request, HttpServletResponse response) {
            Map<String, Object> gmap = new HashMap<String, Object>();
            HttpSession session = ((HttpServletRequest) request).getSession();
            if (session.getAttribute("name") != null) {
                gmap.put("name", session.getAttribute("name").toString());
                gmap.put("login", true);
            } else {
                gmap.put("login", false);
            }

            if (session.getAttribute("usid") != null) {
                gmap.put("usid", session.getAttribute("usid").toString());
            }

            if (session.getAttribute("zw") != null) {
                gmap.put("zw", session.getAttribute("zw").toString());
            }

            if (session.getAttribute("dlzh") != null) {
                gmap.put("dlzh", session.getAttribute("dlzh").toString());
            }

            if (session.getAttribute("uname") != null) {
                gmap.put("uname", session.getAttribute("uname").toString());
            }

            if (session.getAttribute("permission") != null) {
                gmap.put("permission", session.getAttribute("permission").toString());
            }

            if (session.getAttribute("dlma") != null) {
                gmap.put("dlma", session.getAttribute("dlma").toString());
            }

            if (session.getAttribute("sex") != null) {
                gmap.put("sex", session.getAttribute("sex").toString());
            }

            if (session.getAttribute("nj") != null) {
                gmap.put("nj", session.getAttribute("nj").toString());
            }

            if (session.getAttribute("bj") != null) {
                gmap.put("bj", session.getAttribute("bj").toString());
            }

            if (session.getAttribute("year") != null) {
                gmap.put("year", session.getAttribute("year").toString());
            }

            gmap.put("jsessionid", session.getId());
            return gmap;
        }


    @RequestMapping("/filesUpload.html")
    public @ResponseBody
    List<Object> filesUpload(@RequestParam(value = "file") MultipartFile[] files, HttpServletRequest request, HttpSession session) throws IOException {
        List<Object> fileurllist = new ArrayList<Object>();

        Map<String, Object> fileMap = new HashMap<String, Object>();
        if (files != null && files.length > 0) {
            //循环获取file数组中得文件
            for (int i = 0; i < files.length; i++) {
                MultipartFile file = files[i];
                //保存文件
                Map<String, Object> fileurl = new ImgEditor().uploadFilemap(file, request, session);
                fileurllist.add(fileurl);
            }
        }
        return fileurllist;
    }

    @RequestMapping("/downloadfile.html")
    public void downloadfile(HttpServletRequest request, HttpServletResponse resp, HttpSession session) throws IOException {
        String fileurl = request.getParameter("fileurl").trim();
        ExportExcel.downloadfile(resp, request, fileurl);
    }


    @RequestMapping("ZXlogin.html")
    @ResponseBody
    public void ZXlogin(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = ((HttpServletRequest) request).getSession();
        session.removeAttribute("name");
        session.removeAttribute("usid");
        session.removeAttribute("zw");
        session.removeAttribute("dlzh");
        session.removeAttribute("uname");
        session.removeAttribute("permission");
        session.removeAttribute("dlma");
        session.removeAttribute("sex");
        session.removeAttribute("nj");
        session.removeAttribute("bj");
        session.removeAttribute("year");
    }



    @RequestMapping("htlogin.html")
    @ResponseBody
    public Map<String, Object> htlogin(HttpServletRequest request, HttpServletResponse response) {
        String uname = request.getParameter("uname").trim();
        String upwd = request.getParameter("upwd").trim();
        Map<String, Object> dlmap = new HashMap<>();
        dlmap.put("uname", uname);
        dlmap.put("upwd", upwd);
        //查询账号权限
        String permission = loginService.getUserPermission(dlmap);
        //根据用户名和密码查询用户是否存在
        List<Map<String, Object>> list1 = loginService.getUaccountByZhMm(dlmap);
        if (list1.size() > 0) {
            Map<String, Object> rmap = new HashMap<>();
            Map<String, Object> gmap = list1.get(0);
            HttpSession session = ((HttpServletRequest) request).getSession();

            if (gmap.get("USID") != null) {
                session.setAttribute("usid", gmap.get("USID").toString());
            }
            if (gmap.get("NAME") != null) {
                session.setAttribute("name", gmap.get("NAME").toString());
            }
            if (gmap.get("ZW") != null) {
                session.setAttribute("zw", gmap.get("ZW").toString());
            }
            if (gmap.get("WSTATE") != null) {//1学生 2教师
                if (gmap.get("WSTATE").toString().equals("1")) {
                    rmap.put("msg", "账号权限不足!");
                    rmap.put("result", false);
                    return rmap;
                }
            }
            if (gmap.get("DLZH") != null) {
                session.setAttribute("dlzh", gmap.get("DLZH").toString());
            }
            if (gmap.get("DLMA") != null) {
                session.setAttribute("dlma", gmap.get("DLMA").toString());
            }
            if (gmap.get("SEX") != null) {
                session.setAttribute("sex", gmap.get("SEX").toString());
            }
            if (gmap.get("NJ") != null) {
                session.setAttribute("nj", gmap.get("NJ").toString());
            }
            if (gmap.get("BJ") != null) {
                session.setAttribute("bj", gmap.get("BJ").toString());
            }
            if (gmap.get("YEAR") != null) {
                session.setAttribute("year", gmap.get("YEAR").toString());
            }

            rmap.put("usid", gmap.get("USID"));
            rmap.put("name", gmap.get("NAME"));
            rmap.put("zw", gmap.get("ZW"));
            rmap.put("dlzh", gmap.get("DLZH"));
            rmap.put("dlma", gmap.get("DLMA"));
            rmap.put("sex", gmap.get("SEX"));
            rmap.put("nj", gmap.get("NJ"));
            rmap.put("bj", gmap.get("BJ"));
            rmap.put("year", gmap.get("YEAR"));
            rmap.put("jsessionid", session.getId());
            rmap.put("uname", uname);
            session.setAttribute("uname", uname);
            rmap.put("result", true);
            if (permission != null && !permission.equals("undefined") && !permission.equals("")) {
                rmap.put("permission", permission);
                session.setAttribute("permission", permission);
            } else {
                rmap.put("permission", "null");
            }
            return rmap;
        }else{
            Map<String, Object> rmap = new HashMap<>();
            Map<String, Object> getmap=getthirdInfo.getUserInfoThird(uname,upwd);
            if(getmap.get("result").toString().equals("true")){
                Map<String, Object> insmap = new HashMap<>();
                insmap.put("name",getmap.get("name"));
                insmap.put("sex",getmap.get("sex"));
                insmap.put("zw",getmap.get("identity"));
                insmap.put("nj",getmap.get("nj"));
                insmap.put("bj",getmap.get("bj"));
                insmap.put("jlsczt","0");
                insmap.put("dlzh",uname);
                insmap.put("dlma",upwd);
                insmap.put("wstate","2");
                //查询账号是否在系统中存在，如果不存在，则插入，否则修改
                String usid="";
                List<Map<String, Object>> list2 = loginService.getUaccountBydlZhName(insmap);
                if(list2.size()>0){
                    usid=list2.get(0).get("USID").toString();
                    loginService.update_Uaccount(insmap);
                }else{
                    usid= UUID.randomUUID().toString().replaceAll("-", "");
                    insmap.put("usid",usid);
                    loginService.insert_Uaccount(insmap);
                }

                if(getmap.get("user_type").toString().equals("1")){
                    rmap.put("msg", "账号权限不足!");
                    rmap.put("result", false);
                    return rmap;
                }

                HttpSession session = ((HttpServletRequest) request).getSession();
                session.setAttribute("usid", usid);
                if (insmap.get("name") != null) {
                    session.setAttribute("name", insmap.get("name").toString());
                }
                if (insmap.get("zw") != null) {
                    session.setAttribute("zw", insmap.get("zw").toString());
                }
                session.setAttribute("dlzh", uname);
                session.setAttribute("dlma", upwd);
                if (insmap.get("sex") != null) {
                    session.setAttribute("sex", insmap.get("sex").toString());
                }
                if (insmap.get("nj") != null) {
                    session.setAttribute("nj", insmap.get("nj").toString());
                }
                if (insmap.get("bj") != null) {
                    session.setAttribute("bj", insmap.get("bj").toString());
                }

                rmap.put("usid", usid);
                rmap.put("name", insmap.get("name"));
                rmap.put("zw", insmap.get("zw"));
                rmap.put("dlzh", uname);
                rmap.put("dlma", upwd);
                rmap.put("sex",insmap.get("sex"));
                rmap.put("nj", insmap.get("nj"));
                rmap.put("bj", insmap.get("bj"));
                rmap.put("jsessionid", session.getId());
                rmap.put("uname", uname);
                session.setAttribute("uname", uname);
                rmap.put("result", true);
                if (permission != null && !permission.equals("undefined") && !permission.equals("")) {
                    rmap.put("permission", permission);
                    session.setAttribute("permission", permission);
                } else {
                    rmap.put("permission", "null");
                }
                return rmap;
            }else{
                return rmap;
            }
        }
    }






}
